<?php
namespace Home\Controller;
use Home\Model\ManagerModel;
use Home\Model\UserModel;
use Home\Model\OrderModel;
use Home\Model\RecordModel;
use Home\Model\CateModel;
use Home\Model\ClassModel;
class ManagerController extends BaseController {
    private $userSize=0;
    public function __construct(){
        parent::__construct();
       /*  $uid=$_COOKIE['uid'];
        if(empty($uid)||$uid==""){
          $this->redirect("Index/index");
        } */
        $this->userSize=10;
        $where['id']=$uid;
        $manageModel=new ManagerModel();
        $result=$manageModel->getManageItem($where);
        $this->assign("username",$result['username']);  
    }
    
    public function categoryList(){
        $cateModel=new CateModel();
        $cateList=$cateModel->getCateList();
        $this->assign("cateList",$cateList);
        $this->display("Manage/catelist");
    }
    public function detailCate(){
        $id=intval(I("id"));
        if($id>0){
            $cateModel=new CateModel();
            $where['id']=$id;
            $cate=$cateModel->getCate($where);
            $this->assign("cate",$cate);
        }
        $this->display("Manage/cateDetail");
    }
    public function delCate(){
        $id=I("id");
        $cateModel=new CateModel();
        $where['id']=$id;
        $result=$cateModel->delCate($where);
        if($result){               
            $this->redirect('Manager/categoryList');
        }else{
            $this->error("删除失败！");
        }
    }
    public function upCate(){
        $id=intval(I("id"));
        $data['name']=I("name");
        $cateModel=new CateModel();
        if($id>0){
            $where['id']=$id;
            $result=$cateModel->upCate($where,$data);
        }else{
            $result=$cateModel->addCate($data);
        }
        if($result){
            $this->redirect('Manager/categoryList');
        }else{
            $this->error("操作失败！");
        }
       
    }
    public function classList($pageNo = 1){
        $classModel=new ClassModel();
        $condition=I("condition");
        if(!empty($condition)){
            $where=" title like '%".$condition."%'";
        }
        $pageNo =$pageNo <= 0 ? 1:$pageNo;
        $page = $pageNo.','.$this->userSize;
        $classList=$classModel->getClassListByPage($where,$page);
        $pageData =$classList;
        $classCount=$classModel->getclassCount($where);
        $p = new PageController($classCount,$this->userSize,$pageNo,$pageData);
        $pageViewString = $p->echoPageAsDiv();
        $this->assign("classlist",$classList);
        $this->assign("condition",$condition);
        $this->assign('page',$pageViewString);
        $this->display("Manage/classlist");
    }
    public function detailClass(){
        $id=intval(I("id"));
        if($id>0){
            $classModel=new ClassModel();
            $where['id']=$id;
            $classInfo=$classModel->getClassItem($where);
            $classInfo['starttime']=date("Y-m-d H:i:s",$classInfo['starttime']);
            $classInfo['cateid']=intval($classInfo['cateid']);
            $this->assign("classInfo",$classInfo);
        } 
        $userModel=new UserModel();
        $uwhere['status']=2;
        $userList=$userModel->getUserlist($uwhere);
        $cateModel=new CateModel();
        $cateList=$cateModel->getCateList();
        $this->assign("cateList",$cateList);
        $this->assign("userList",$userList);
        $this->display("Manage/classDetail");
    }
    public function delClass(){
        $id=intval(I("id"));
        $classModel=new ClassModel();
        $where['id']=$id;
        $result=$classModel->delClass($where);
        if($result){
            $this->redirect("Manager/classList");
        }else{
            $this->error("操作失败");
        }
    }
    public function postClass(){
        $id=intval(I("id"));
        $data=I("class");
        $classModel=new ClassModel();
        if($id>0){
            $where['id']=$id;
            $result=$classModel->upClass($where,$data);
        }else{
            $result=$classModel->addClass($data);
        }
        if($result){
            $this->redirect("Manager/classList");
        }else{
            $this->error("操作失败");
        }
    }

    public function userList($pageNo = 1){
        $userModel=new UserModel();
        $condition=I("condition");
        $where=" status=1";
        if(!empty($condition)){
            $where.=" and nickname like '%".$condition."%'";
        }
        $pageNo =$pageNo <= 0 ? 1:$pageNo;
        $page = $pageNo.','.$this->userSize;
        $userList=$userModel->getUserListByPage($where,$page);
        $pageData =$userList;
        $userCount=$userModel->getuserCount($where);
        $p = new PageController($userCount,$this->userSize,$pageNo,$pageData);
        $pageViewString = $p->echoPageAsDiv();
        $this->assign("userlist",$userList);
        $this->assign("condition",$condition);
        $this->assign('page',$pageViewString);
        $this->display("Manage/userlist");
    }
    public function setTeach(){
        $id=I("id");
        $where['id']=$id;
        $data['status']=2;
        $userModel=new UserModel();
        $result=$userModel->UpUser($where,$data);
        if($result){
                $this->redirect("Manager/userList");
        }else{
                $this->error("操作失败");
        }
    }
    public function setCompere(){
        $id=I("id");
        $where['id']=$id;
        $data['status']=3;
        $userModel=new UserModel();
        $result=$userModel->UpUser($where,$data);
        if($result){
            $this->redirect("Manager/userList");
        }else{
            $this->error("操作失败");
        }
    }
    
    
    public function teachList($pageNo = 1){
        $userModel=new UserModel();
        $condition=I("condition");
        $where=" status=2";
        if(!empty($condition)){
            $where.=" and nickname like '%".$condition."%'";
        }
        $pageNo =$pageNo <= 0 ? 1:$pageNo;
        $page = $pageNo.','.$this->userSize;
        $userList=$userModel->getUserListByPage($where,$page);
        $pageData =$userList;
        $userCount=$userModel->getuserCount($where);
        $p = new PageController($userCount,$this->userSize,$pageNo,$pageData);
        $pageViewString = $p->echoPageAsDiv();
        $this->assign("userlist",$userList);
        $this->assign("condition",$condition);
        $this->assign('page',$pageViewString);
        $this->display("Manage/teacherlist");
    }
    public function compereList($pageNo = 1){
        $userModel=new UserModel();
        $condition=I("condition");
        $where=" status=3";
        if(!empty($condition)){
            $where.=" and nickname like '%".$condition."%'";
        }
        $pageNo =$pageNo <= 0 ? 1:$pageNo;
        $page = $pageNo.','.$this->userSize;
        $userList=$userModel->getUserListByPage($where,$page);
        $pageData =$userList;
        $userCount=$userModel->getuserCount($where);
        $p = new PageController($userCount,$this->userSize,$pageNo,$pageData);
        $pageViewString = $p->echoPageAsDiv();
        $this->assign("userlist",$userList);
        $this->assign("condition",$condition);
        $this->assign('page',$pageViewString);
        $this->display("Manage/comperelist");
    }
    
    public function TsetUser(){
        $id=I("id");
        $where['id']=$id;
        $data['status']=1;
        $userModel=new UserModel();
        $result=$userModel->UpUser($where,$data);
        if($result){
            $this->redirect("Manager/teachList");
        }else{
            $this->error("操作失败");
        }
    }
    
    public function CsetUser(){
        $id=I("id");
        $where['id']=$id;
        $data['status']=1;
        $userModel=new UserModel();
        $result=$userModel->UpUser($where,$data);
        if($result){
            $this->redirect("Manager/compereList");
        }else{
            $this->error("操作失败");
        }
    }
    
    public function userInfo(){
        $uid=I("id");
        $userModel=new UserModel();
        $where['id']=$uid;
        $userInfo=$userModel->getUserItem($where);
        $this->assign("userInfo",$userInfo);
        $this->display("Manager/userDetail");
    }
    
}